Page({
  data: {
    id: wx.getStorageSync('userid'),
    list:[],
    dingdan:''
  },

  onLoad: function (options) {
    this.setData({
      id: wx.getStorageSync('userid')
    })
    wx.request({
      url: `https://www.prebeauty.work:3001/api/goods?goodsid=${options.goodsid}`,
      header: {
        'content-type': 'application/json'
      },
      success:async (res)=>{
        console.log(res.data)
        this.setData({
          list:res.data[0]
        })
        console.log(this.data.list)
      }
    })
  },

  addgift:function(e){
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res){
          if(res.confirm){
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home',
            })
          }else if(res.cancel){
            console.log('取消')
          }
        }
      })
    }
    console.log(e.currentTarget.dataset.id)
    wx.request({
        url: 'https://www.prebeauty.work:3001/api/cart',
        data: {
          goodsid:this.data.list.goodsid,
          shopid:this.data.list.shopid,
          goodsname:this.data.list.goodsname,
          goodsprice:this.data.list.goodsprice,
          goodsimg:this.data.list.goodsimg,
          goodsbuynum:1,
          userid:this.data.id
        },
        header: {
          'content-type': 'application/json'
        },
        method: 'POST',
        success: (res)=>{
          console.log('add success')
          wx.showToast({
            title: '添加成功！',
            icon: 'success',
            duration: 2000
          })
        }
      })
  },

  tobuy:function(e){
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res){
          if(res.confirm){
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home',
            })
          }else if(res.cancel){
            console.log('取消')
          }
        }
      })
    }
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/dingdan',
      data: {
        goodsid:this.data.list.goodsid,
        goodsname:this.data.list.goodsname,
        price:this.data.list.goodsprice,
        pay:'未支付',
        goodsimg:this.data.list.goodsimg,
        goodsnum:1,
        userid:this.data.id,
        type:1
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: (res)=>{
        console.log(res.data)
        console.log('create dingdan')
        this.setData({
          dingdan: res.data
        })
        console.log(this.data)
        wx.navigateTo({
          url: `/shop/buy/buy?id=${this.data.dingdan}`,
        })
      }
    })
  },

  tosong:function(e){
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res){
          if(res.confirm){
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home',
            })
          }else if(res.cancel){
            console.log('取消')
          }
        }
      })
    }
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/dingdan',
      data: {
        goodsid:this.data.list.goodsid,
        goodsname:this.data.list.goodsname,
        price:this.data.list.goodsprice,
        pay:'未支付',
        goodsimg:this.data.list.goodsimg,
        goodsnum:1,
        userid:this.data.id,
        type:2
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: (res)=>{
        console.log(res.data)
        console.log('create dingdan')
        this.setData({
          dingdan: res.data
        })
        console.log(this.data)
        wx.navigateTo({
          url: `/shop/buy/buy?id=${this.data.dingdan}`,
        })
      }
    })
  }
})